package com.stb.llh.controller;

import com.stb.llh.config.UserConfig;
import com.stb.llh.entity.Admin;
import com.stb.llh.entity.Emotion;
import com.stb.llh.entity.PreLife;
import com.stb.llh.entity.User;
import com.stb.llh.service.AdminService;
import com.stb.llh.util.ResJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author Lee
 * @Date 2021/11/7
 * @Description
 */

@Controller
@RequestMapping("/admin")
public class AdminController {


    @Autowired
    private AdminService adminService;




    @GetMapping("/outLogin")
    public String outLogin(HttpServletRequest req){

        if (req.getSession().getAttribute("admin") != null) {
            req.getSession().invalidate();
            return "redirect:/adminLogin";
        }
        return "redirect:/adminLogin";
    }

    @PostMapping("/loginRes")
    @ResponseBody
    public String loginRes(HttpServletRequest req, HttpServletResponse res, String username, String pwd) {
        if ("post".equalsIgnoreCase(req.getMethod())) {
            System.out.println(username);
            System.out.println(pwd);

            Admin admin = adminService.doLogin(username, pwd);
            System.out.println(admin);
//            User u = userDao.findById(1).get();

            if (admin == null) {
                return ResJson.getError("登录失败");

            }
            req.getSession().setAttribute("admin", admin);
            System.out.println(ResJson.getSuccess("登录成功"));
            return ResJson.getSuccess("登录成功");
        }
        return ResJson.getError("登录失败");
    }


    @GetMapping("/index")
    public String index(){
        return "admin/index";
    }


    @GetMapping("/userManager")
    public String userManager(HttpServletRequest req){

        List<User> userList = adminService.getUserList();
        System.out.println(userList);
        req.setAttribute("users",userList);

        return "admin/userManager";
    }

    @GetMapping("/main")
    public String main(){
        return "admin/main";
    }

    @GetMapping("/updateUser")
    public String updateUser(HttpServletRequest req,User user){
        req.setAttribute("u",user);
        return "admin/updateUser";
    }

    @PostMapping("/updateUserRes")
    @ResponseBody
    public String updateUserRes(HttpServletRequest req,User user){

        System.out.println(user);
        try {
            adminService.updateUser(user);
            return ResJson.getSuccess("ok");
        }catch (Exception e) {

            return ResJson.getError("error");
        }
    }


    @GetMapping("/delUser")
    public String delUser(HttpServletRequest req,int id){

        System.out.println(id);
        if (!adminService.delUser(id)) {
//            List<User> userList = adminService.getUserList();
//            System.out.println(userList);
//            req.setAttribute("users",userList);
            return "redirect:/admin/userManager";
        }

        return "redirect:/admin/userManager";

    }

    @GetMapping("/vent")
    public String vent(HttpServletRequest req){

        List<Emotion> emotionList = adminService.getEmotionList();
        emotionList.sort((o1,o2)->o2.getDates().compareTo(o1.getDates()));
        req.setAttribute("emo",emotionList);
        return "admin/vent";
    }


    @GetMapping("/delEmo")
    public String delEmo(int id){

        if (adminService.delEmo(id)) {
            return "redirect:/admin/vent";
        }
        return "redirect:/admin/vent";

    }

    @GetMapping("/userTest")
    public String userTest(HttpServletRequest req){
        List<PreLife> preLifeList = adminService.getPreLifeList();
        req.setAttribute("lifes",preLifeList);
        return "admin/lifes";
    }

    @GetMapping("/delLife")
    public String delLife(int id){
        if (adminService.delLife(id)) {
            return "redirect:/admin/userTest";
        }
        return "redirect:/admin/userTest";
    }

    @GetMapping("/bigScreen")
    public String bigScreen(HttpServletRequest req,HttpServletResponse res) throws UnsupportedEncodingException {

//      用户数量分析
        List<Integer> userCount = adminService.getUserCount();
        System.out.println(userCount);
        req.setAttribute("userCount",userCount);

//        性别分析
        List<Integer> sexCount = adminService.getSexCount();
        System.out.println(sexCount);
        req.setAttribute("sexCount",sexCount);

        System.out.println(adminService.getAddressCount());

//        地址数量
        List<String> addressCount = adminService.getAddressCount();

        System.out.println(addressCount);
        req.setAttribute("addressCount",addressCount);


//        吐槽数量
        List<Integer> emotionCount = adminService.getEmotionCount();
        System.out.println(emotionCount);
        req.setAttribute("emotionCount",emotionCount);


//        吐槽数据
        List<Emotion> emotionList = adminService.getEmotionList();
        emotionList.sort((o1,o2)->o2.getDates().compareTo(o1.getDates()));
        System.out.println(emotionList);
        req.setAttribute("emotionList",emotionList);

//        测试寿命数据
        List<PreLife> preLifeList = adminService.getPreLifeList();
        System.out.println(preLifeList);
        req.setAttribute("preLifeList",preLifeList);

//        在线人数
        HttpSession session = req.getSession();
        AtomicInteger count = UserConfig.count;
        System.out.println(count.get());
        req.setAttribute("nowUserCount", count.get());

//        天气
        res.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");

        String url = "http://localhost:5000/weather";
        RestTemplate restTemplate = new RestTemplate();

        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url,String.class);
        String body = responseEntity.getBody();

        System.out.println(body);
        req.setAttribute("weather",body);


        return "bigScreen/index";
    }

    @RequestMapping("/a")
    @ResponseBody
    public String a(HttpServletRequest req){
        HttpSession session = req.getSession();
        return "在线人数："+UserConfig.count;
    }



    @PostMapping("/sexCount")
    @ResponseBody
    public String sexCount(){

        return "";
    }





}
